package com.example.factory.core.domain;

import org.apache.commons.collections4.CollectionUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei Tao
 */
@Entity
public class RoleEntity extends DomainObject {
    private String name;
    /**
     * 后台登陆
     */
    @Column(name = "is_admin")
    private boolean isAdmin;

    @OneToMany(mappedBy = "role", fetch = FetchType.LAZY, orphanRemoval = true)
    private List<UserEntity> userEntities;

    public RoleEntity() {
    }

    public UserEntity getUserEntities() {
        if (CollectionUtils.isEmpty(this.userEntities)) {
            return null;
        }
        return userEntities.get(0);
    }

    public void setUserEntities(UserEntity userEntity) {
        if (this.userEntities == null) {
            this.userEntities = new ArrayList<>();
        }
        this.userEntities.add(userEntity);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isAdmin() {
        return isAdmin;
    }

    public void setAdmin(boolean admin) {
        isAdmin = admin;
    }
}
